/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jojo.home.wallet.bean.um;

import java.util.List;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import jojo.home.wallet.entity.um.FwUser;
import jojo.home.wallet.local.um.FwUserLocal;

/**
 *
 * @author Ganduruu
 */
@Stateless
@Local(FwUserLocal.class)
public class FwUserBean implements FwUserLocal {

    @PersistenceContext
    private EntityManager em;

    @Override
    public FwUser getUser(Long userId) {
        if (userId == null) {
            return null;
        }

        FwUser entity = em.find(FwUser.class, userId);

        return entity;
    }

    @Override
    public FwUser login(String username, String password) {
        if (username == null || password == null) {
            return null;
        }

        Query q = em.createNativeQuery("SELECT * FROM FW_USER", FwUser.class);
        Query q1 = em.createNativeQuery("SELECT * FROM FW_USER US WHERE US.USERNAME = " + "'" + username + "'"
                + "AND US.PASSWORD_HASH = " + "'" + password + "'", FwUser.class);

        List<FwUser> users = (List<FwUser>) q1.getResultList();

        return users.get(0);
    }

    @Override
    public List<FwUser> getUserList() {
        List<FwUser> users = em.createQuery("SELECT c FROM FwUser c", FwUser.class).getResultList();
        return users;
    }

}
